package edu.ccut.saturn.studio.logic.model.saturn;

import java.io.File;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.eclipse.draw2d.geometry.Point;
import org.eclipse.jface.dialogs.Dialog;
import org.eclipse.swt.graphics.Image;

import edu.ccut.saturn.studio.core.search.IObserver;
import edu.ccut.saturn.studio.core.search.SaturnFileSearcher;
import edu.ccut.saturn.studio.core.search.SaturnFileSearcher.SearchResult;
import edu.ccut.saturn.studio.core.util.eclipse.Messages;
import edu.ccut.saturn.studio.core.util.freemaker.FreemarkerUtils;
import edu.ccut.saturn.studio.dialog.createcomponentdialog.SaturnCreateComponentDialog;
import edu.ccut.saturn.studio.export.createhtml.Parameters;
import edu.ccut.saturn.studio.logic.model.Shape;
import edu.ccut.saturn.studio.util.ConversionUtil;

public class SaturnCreatComponentNode extends SaturnBaseComponentNode {

	private static final long serialVersionUID = 1L;
	public static final String SHOW_NODE_CREATECOMPONENT = Messages
			.getString("SaturnCreatComponentNode.CreateParameter"); //$NON-NLS-1$

	private static int number = 1;

	private String key = "edu.ccut.saturn.basecomponent::/dataOperationComponent/CreateParameter.component";

	private String fullKey = "";

	public void fire() {
		firePropertyChange(PROPERTERY_CHANGS, null, "");
	}

	public SaturnCreatComponentNode() {
		super.setName(SHOW_NODE_CREATECOMPONENT + "" + number++);
		iniParameter();
	}

	public Dialog getDialog() {
		SaturnCreateComponentDialog dialog = new SaturnCreateComponentDialog(
				super.getShell(), this);
		return dialog;
	}

	@Override
	public String getException() {
		return "";
	}

	@Override
	public String getType() {
		return "SaturnCreatComponentNode";
	}

	private void iniParameter() {
		SaturnParameter parameter1 = new SaturnParameter();
		parameter1.setId("1");
		parameter1.setName("type");
		parameter1.setClassType("java.lang.String");
		parameter1.setPassingType("in");
		parameter1.setScope("1");
		parameter1.setDescription("");

		SaturnParameter parameter2 = new SaturnParameter();
		parameter2.setId("2");
		parameter2.setName("fullKey");
		parameter2.setClassType("java.lang.String");
		parameter2.setPassingType("in");
		parameter2.setScope("1");
		parameter2.setDescription("");

		SaturnParameter parameter3 = new SaturnParameter();
		parameter3.setId("3");
		parameter3.setName("newKey");
		parameter3.setClassType("java.lang.String");
		parameter3.setPassingType("out");
		parameter3.setScope("1");
		parameter3.setDescription("");

		super.getParameters().add(parameter1);
		super.getParameters().add(parameter2);
		super.getParameters().add(parameter3);
	}

	@Override
	public String getFullKey() {
		return ConversionUtil.quotationRemove(this.fullKey);
	}

	public void setFullKey(String fullKey) {
		if (!this.fullKey.equals(fullKey)) {
			this.fullKey = fullKey;
			firePropertyChange(PROPERTERY_CHANGS, null, fullKey);
		}
	}

	@Override
	public String getKey() {
		return key;
	}

	public void setKey(String key) {
		if (!this.key.equals(key)) {
			this.key = key;
			firePropertyChange(PROPERTERY_CHANGS, null, key);
		}
	}

	@Override
	public Shape cloneShape(Point mousePoint, Shape shape) {
		SaturnCreatComponentNode saturnComponentNode = new SaturnCreatComponentNode();
		SaturnCreatComponentNode saturn = (SaturnCreatComponentNode) shape;
		saturnComponentNode.setLocation(mousePoint);
		saturnComponentNode.setKey(saturn.getKey());
		saturnComponentNode.setName(saturn.getName());
		saturnComponentNode.setDescription(saturn.getDescription());
		saturnComponentNode.setParameters(saturn.getParameters());
		saturnComponentNode.setException(saturn.getException());
		saturnComponentNode.setFullKey(saturn.getFullKey());
		saturnComponentNode.setId(saturn.getId());
		saturnComponentNode.setNextId(saturn.getNextId());
		return saturnComponentNode;
	}

	@Override
	protected void setImage(Image image) {
	}
	
	public List<SearchResult> keyMapInWorkspace(IObserver observer) {
		
		List<SearchResult> list = null;
		String[] suffixes = {"saturndata"};
		
		try {
			list = SaturnFileSearcher.getValueAndDocumentMapInWorkspace(suffixes , observer, null);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return list;
	}
	
	
	public List<SearchResult> keyMapInProject(IObserver observer) {
		List<SearchResult> list = null;
		
		try {
			list = SaturnFileSearcher.getValueAndDocumentMapInProject(super.findTheProject(), "saturndata",
					observer, null);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return list;
}

	@Override
	public Parameters createNodeHtml(String filePath, String[] tempnames) {

		Map<String, String> infos = new HashMap<String, String>();
		String name = getName();
		Parameters para = new Parameters();

		infos.put("name", getName());
		infos.put("id", getId());
		infos.put("key", getKey());
		infos.put("generateParameter", getParameters()
				.get(2).getValueKey());
		infos.put("parameterType", getParameters().get(0)
				.getValueKey().replaceAll("'", ""));
		infos.put("fullyKey", getFullKey());
		infos.put("description", getDescription());

		para.setInfourl(name + "-createComponentnode.html");
		para.setInfoname(name);
		para.setDescription(getDescription());
		para.setNodeType(Messages.getString("Logic.CreateParaNode")); //$NON-NLS-1$
		para.setImagename("createComponentnode");
		String templatePath = "template/export/logic/creatcomponent";
		String Path = filePath + File.separator + name + "-createComponentnode.html";
		FreemarkerUtils.writeTemplateToFile(templatePath, infos, Path);

		return para;
		
	}
}
